//
//  K_MyroomVC.m
//  K-Music
//
//  Created by CHUN on 16/1/25.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "K_MyroomVC.h"
#import "K_ROOMCell.h"
#import "K_Mode.h"
#import "New_ROOMVC.h"
@interface K_MyroomVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    
}
@property(nonatomic,strong)UICollectionView * collectionView;


@property (nonatomic ,strong) UITableView * tableView;


@end

@implementation K_MyroomVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的房间";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    [self creatUI];

}
- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -creatUI-
- (void)creatUI
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH  , HEIGHT-64)collectionViewLayout:flowLayout];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    _collectionView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:_collectionView];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return [User_information conversion].MYROOM_Data.count+1;
    
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",(long)indexPath.row);
    
    [_collectionView registerClass:[K_ROOMCell class] forCellWithReuseIdentifier:@"K_ROOMCell"];
    
    K_ROOMCell*cell=(K_ROOMCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"K_ROOMCell" forIndexPath:indexPath];
    
    if (indexPath.row == [User_information conversion].MYROOM_Data.count) {
        UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH/2, HEIGHT/4)];
        view.backgroundColor = RGB(241, 241, 241);
        
        UIImageView * add_icon = [[UIImageView alloc]initWithFrame:CGRectMake(view.width/2-40, 40, 80, 80)];
        
        add_icon.image = [UIImage imageNamed:@"add_P"];
        
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(add_icon.frame)+10, view.width, 30)];
        label.text = @"创建房间";
        label.font = [UIFont systemFontOfSize:13];
        label.textAlignment = 1;
        [view addSubview:label];
        
        [view addSubview:add_icon];
        
        [cell addSubview:view];
        
    }
    else
    {
        K_Mode * data =[[K_Mode alloc]init];
        
        data  = [User_information conversion].MYROOM_Data[indexPath.row];
        
        cell.mylabel.text=[NSString stringWithFormat:@"%@ 人",data.peopleNumber];
        
        cell.my_name.text=[NSString stringWithFormat:@"%@",data.descriptionD];
        
        cell.room_num_label.text=[NSString stringWithFormat:@"%@",data.groupId];
        
        [cell.dt sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.pictureUrl]]];
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%ld",indexPath.row);
    if (indexPath.row == [User_information conversion].MYROOM_Data.count ) {
        
        New_ROOMVC * room = [[New_ROOMVC alloc]init];
        [self.navigationController pushViewController:room animated:YES];
        
    }
}

//定义每一个cell大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(WIDTH/2-2, HEIGHT/4);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
